package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    @Transactional //涉及到插入两张表所以需要使用事务
    public void saveWithFlavor(DishDTO dishDTO) {
        //属性拷贝
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        // 插入菜品主体数据
        Long id = dishMapper.insert(dish);

        //插入口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(dishFlavor -> dishFlavor.setDishId(id));

        //插入口味信息
        dishMapper.insertFlavors(flavors);
    }

    /**
     * 分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public DishPageDTO pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页查询:{}", dishPageQueryDTO);
        //设置分页查询参数
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        //执行查询语句
        Page<DishVO> page = dishMapper.page(dishPageQueryDTO);
        return new DishPageDTO(page.getTotal(), page.getResult());
    }

    /**
     * 删除菜品
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteById(List<Long> ids) {
        //起售中的菜品不能删除
        //查询是否在起售状态 1起售 0停售
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //被套餐关联的菜品不能删除
        Long count = setmealMapper.getByIdWithSetmealDish(ids);
        log.info("被套餐关联的菜品数量为：{}", count);
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品后 关联的口味数据也要删除
        setmealMapper.deleteByIdWithdish(ids);

        //删除关联的口味数据
        setmealMapper.deleteByIdWithFlavor(ids);

    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @Override
    public DishDTO getById(Long id) {
        //先查询主题数据
        Dish dish = dishMapper.getById(id);
        //拷贝数据到DishDTO
        DishDTO dishDTO = new DishDTO();
        BeanUtils.copyProperties(dish, dishDTO);


        //然后查询口味数据
        List<DishFlavor> flavors = dishMapper.getByIdWithFlavor(id);
        dishDTO.setFlavors(flavors);

        return dishDTO;
    }

    /**
     * 修改菜品数据
     *
     * @param dishDTO
     */
    @Override
    @Transactional  //事务同步
    public void updateWithFlavor(DishDTO dishDTO) {
        //修改主体数据
        dishMapper.updateById(dishDTO); //如果有数据就插入

        //先删除所有的口味数据
        dishMapper.deleteByIdWithFlavor(dishDTO.getId());

        //然后增加所有的口味数据
        if (dishDTO.getFlavors() != null && dishDTO.getFlavors().size() > 0) {
            dishMapper.insertFlavors(dishDTO.getFlavors());
        }
    }

    /**
     * 根据分类id查询所有的菜品数据
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<DishDTO> list(Integer categoryId) {
        log.info("根据分类id查询所有的菜品数据:{}", categoryId);
        List<DishDTO> dishDTOList = dishMapper.list(categoryId);
        return dishDTOList;
    }

    /**
     * 条件查询菜品和口味 根据分类id
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {

        //查询主体数据
        List<Dish> dishList = dishMapper.listBaseinfo(dish.getCategoryId());
        log.info("查询到的菜品数据：{}", dishList);

        //查询口味数据
        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);
            log.info("查询到的菜品数据：{}", dishVO);
            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    /**
     * 菜品起售停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        dishMapper.startorstop(status, id);
    }
}
